/*
 * Rhy String Core Lib
 * 
 * @author Tristan Maindron
 */

Rhy.string = {
    
    /*
     * Cut a string to be maximum size under a max length. Do not cut in middle
     * of the words. The third argument is the string to append. By default "...".
     */
	ellipsis: function(t, len, a){
		if(t.length > len){
			var i = t.lastIndexOf(' ', len);
			t = t.substr(0, i) + (typeof(a) == 'undefined' ? '...' : a);
		}
		return t;
	},
    
    isEmpty: function(str) {
        return (!str || 0 === str.length);
    },
    
    isBlank: function(str) {
        return (!str || /^\s*$/.test(str));
    },
    
    utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }
};

